﻿namespace PortableRiotGames.Infrastructure.Converters
{
    using Newtonsoft.Json;
    using System;

    public class JsonRankedQueueConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            throw new NotImplementedException();
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.Value.ToString() == "RANKED_SOLO_5x5") return RankedQueue.RankedSolo5x5;
            if (reader.Value.ToString() == "RANKED_TEAM_3x3") return RankedQueue.RankedTeam3x3;
            if (reader.Value.ToString() == "RANKED_TEAM_5x5") return RankedQueue.RankedTeam5v5;
            return RankedQueue.RankedSolo5x5;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var queue = (RankedQueue)(Enum.Parse(typeof(RankedQueue), value.ToNullableString()));

            var valueToSerialize = string.Empty;

            if (queue == RankedQueue.RankedSolo5x5) valueToSerialize = "RANKED_SOLO_5x5";
            else if (queue == RankedQueue.RankedTeam3x3) valueToSerialize = "RANKED_TEAM_3x3";
            else if (queue == RankedQueue.RankedTeam5v5) valueToSerialize = "RANKED_TEAM_5x5";

            serializer.Serialize(writer, valueToSerialize);
        }
    }
}
